﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ma.Bussiness.Public
{
    public static class FldSeach
    {
        public static void Show()
        {
            var conditions = new List<Condition>
        {
            new Condition { FldName = "Name", SearchType = ComparisonType.Equal, KeyWord = "Tom" },
            new Condition { FldName = "Value", SearchType = ComparisonType.NotEqual, KeyWord = "2" }
            // 可以添加更多的条件...
        };
            // 调用方法拼接委托并进行查询
            Expression<Func<Data, bool>> result = QuerySeach.QueryData<Data>(conditions);
            List<Data> datas = new List<Data>
            {
                new Data() { Name = "Tom", Value = "1" },
                new Data() { Name = "Tom2", Value = "2" },
                new Data() { Name = "Tom3", Value = "3" },
                new Data() { Name = "Tom", Value = "4" }
            };
            Console.WriteLine(result.ToString());
            Console.WriteLine(result.Compile().ToString());
            var rr = datas.Where(result.Compile()).ToList();
           
        }
    }
    internal class QuerySeach
    {


        // 定义拼接条件的方法
        internal static Expression<Func<T, bool>> QueryData<T>(List<Condition> conditions) where T : class
        {
            // 创建参数表达式
            var parameter = Expression.Parameter(typeof(T), "a");

            // 创建初始条件为 true 的表达式
            Expression<Func<T, bool>> filterExpression = Expression.Lambda<Func<T, bool>>(Expression.Constant(true), parameter);

            // 动态构建 Lambda 表达式
            foreach (var item in conditions)
            {
                // 获取属性信息
                var property = typeof(T).GetProperty(item.FldName);

                // 创建属性访问表达式
                var propertyAccess = Expression.Property(parameter, property);

                // 创建常量表达式
                var constant = Expression.Constant(Convert.ChangeType(item.KeyWord, property.PropertyType));

                // 创建对应的比较表达式
                Expression comparison;
                switch (item.SearchType)
                {
                    case ComparisonType.Equal:
                        comparison = Expression.Equal(propertyAccess, constant);
                        break;
                    case ComparisonType.NotEqual:
                        comparison = Expression.NotEqual(propertyAccess, constant);
                        break;
                    // 可以添加更多的比较类型的处理...
                    default:
                        throw new ArgumentException("Unsupported comparison type.");
                }

                // 使用 AndAlso 连接多个条件
                filterExpression = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(filterExpression.Body, comparison), parameter);
            }

            // 编译表达式并应用于数据集
            return filterExpression;
        }

    }
    internal class Condition
    {
        public string FldName { get; set; }
        public ComparisonType SearchType { get; set; }
        public string KeyWord { get; set; }
    }
    internal class Data
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    internal enum ComparisonType
    {
        Equal,
        NotEqual,
        GreaterThan,
        LessThan
        // 可以添加更多的比较类型...
    }
}
